package org.framework.test;

import java.io.*;
import java.util.Properties;

import org.apache.tools.ant.*;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.types.*;

/**
 * 调用 ant.jar 的 SQLExec 执行 SQL 脚本文件
 */
public class DbTools extends SQLExec {
	
	public static Properties props = new Properties();
	private static final String configFile ="conf/db.properties";
	
	public void init(){
		super.init();
		try {
			props.load(new FileInputStream(configFile));
			String driver = props.getProperty("driver");
			String userid = props.getProperty("userid");
//			System.out.println("driver="+driver);
//			System.out.println("userid="+userid);
			// 设置数据库参数
			this.setDriver(driver);
			this.setUrl(props.getProperty("url"));
			this.setUserid(userid);
			this.setPassword(props.getProperty("password"));
			this.setEncoding(props.getProperty("encoding"));
			this.setOnerror((SQLExec.OnError) (EnumeratedAttribute.getInstance(
					SQLExec.OnError.class, props.getProperty("onerror"))));
			this.setPrint(true); 
//			// 输出到文件 sql.out 中；不设置该属性，默认输出到控制台
//			this.setOutput(new File("sql/sql.out"));
			
			// 要指定这个属性，不然会出错
			this.setProject(new Project()); 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setSrc(String scriptName){
		this.setSrc(new File(scriptName));
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	}
	
}